package com.mxf.example.utils.tools;




import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.regex.Pattern;

/**
 * 用于验证某字符串是否含于某字符串集合中的某一项
 */
public class CollectionTools {
    public static boolean containStr(Collection<String> collections, String str) {
      /*  /doc.html:/example/doc.html*/
        System.out.println( Pattern.matches("/doc","/example/doc.html"));
        for (String url : collections) {
            System.out.println(escapeExprSpecialWord(url)+":"+str);
            if ( Pattern.matches(escapeExprSpecialWord(url),str)) {
                return true;
            } else {
                continue;
            }
        }
        return false;
    }

    /**
     * 转义正则特殊字符 （$()*+.[]?\^{},|）
     *
     * @param keyword
     * @return
     */
    public static String escapeExprSpecialWord(String keyword) {
        if (!StringUtils.isEmpty(keyword)) {
            String[] fbsArr = {"\\", "$", "(", ")", "*", "+",  "[", "]", "?", "^", "{", "}", "|"};
            for (String key : fbsArr) {
                if (keyword.contains(key)) {
                    keyword = keyword.replace(key, "\\" + key);
                }
            }
        }
        return keyword;
    }
    public static String returnString(String str){
        String reg = "\\d+";
        String reg1 = "\\\\:";
        String reg2 = "\\\\+";
        String reg3 = "\\\\(";
        String reg4 = "\\\\)";
        String reg5 = "\\\\[";
        String reg6 = "\\\\]";
        String reg7 = "\\\\$";
        String reg8 = "\\\\*";
        String tmp = str.replaceAll("\\+",reg2);
        String tmp1 = tmp.replaceAll(reg,"\\\\d+");
        String tmp2 = tmp1.replaceAll("\\:",reg1);
        String tmp3 = tmp2.replaceAll("\\(",reg3);
        String tmp4 = tmp3.replaceAll("\\)",reg4);
        String tmp5 = tmp4.replaceAll("\\[",reg5);
        String tmp6 = tmp5.replaceAll("\\]",reg6);
        String tmp7 = tmp6.replaceAll("\\$",reg7);
        String tmp8 = tmp7.replaceAll("\\*",reg8);
        return tmp8;
    }
}
